package com.bluebench.bigul.domain.graphics;

import java.util.List;

import com.bluebench.bigul.exception.GraphicInitializationException;
import com.itextpdf.text.pdf.PdfContentByte;

public class Line extends Graphic{
	
	private float length;
	
	public Line(Point p1, Point p2) {
		vertices.add(p1);
		vertices.add(p2);
	}
	
	public Line(PdfContentByte canvas) {
		super(canvas);	
	}
	
	public Line(float l){
		length=l;
	}
	
	@Override
	public Graphic build() throws GraphicInitializationException {
		if(canvas==null){
			throw new GraphicInitializationException();
		}
		return null;
	}

	public float getLength() {
		return length;
	}

	public void setLength(float length) {
		this.length = length;
	}

	@Override
	protected boolean validate() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected Graphic concentric(float distance, boolean in) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Graphic copy(float distance, int direction) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public float getSlope(){
		Float slope=null;
		float deltaY = vertices.get(1).getY() - vertices.get(0).getY();
		float deltaX = vertices.get(1).getX() - vertices.get(0).getX();
		
		if(deltaY==0){
			return 0;
		}else{
			slope = (deltaY)/(deltaX);
		}
		
		return slope;
		
	}
	
	public float getYIntercept(){
		
		float x = vertices.get(0).getX();
		float y = vertices.get(0).getY();
		
		float m1 = getSlope();
		
		return (y - (m1*x));
	}
	
	public Point getIntersection(Line l){
		
		float m1 = getSlope();
		float m2 = l.getSlope();
		
		float c1 = getYIntercept();
		float c2 = l.getYIntercept();
		
		float x = (c1 - c2)/(m2-m1);
		float y = (c1*m2 + c2*m1)/(m1-m2);
		
		Point p = new Point(x,y);
		
		return p;		
	}
	
	public Point getMidpoint(){
		float x0 = vertices.get(0).getX();
		float y0 = vertices.get(0).getY();
		
		float x1 = vertices.get(1).getX();
		float y1 = vertices.get(1).getY();
		
		float midX = x0 + ((x1 - x0)/2);
		float midY = y0 + ((y1 - y0)/2);
		
		return new Point(midX,midY);
	}
	
	public Float computeLength(){
		float x0 = getVertices().get(0).getX();
		float y0 = getVertices().get(0).getY();
		
		float x1 = getVertices().get(1).getX();
		float y1 = getVertices().get(1).getY();
		
		float length = (float)Math.sqrt((Math.pow((x1-x0),2) + Math.pow((y1-y0),2))); 
		
		return length;
	}
	

}
